<?php
/**
 * Project     Newsletter for Joomla 1.5.x
 * Release     2.0.1
 * @package    Communicator
 * @subpackage Component
 * @version    $Id$
 * @link       http://joomlacode.org/gf/project/communicator/
 * @copyright  Copyright (c) 2008 Stefan Granholm, Copyright 2009 Erik Damke - All rights reserved
 * @license    http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 *
 * @Author     Stefan Granholm (http://www.granholmcms.com), Erik Damke (commqc20@jocoflex.com)
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

function CommunicatorBuildRoute( &$query )
{
	$segments = array();
	
	
	if (isset($query['task'])) {
		$segments[] = $query['task'];
		unset( $query['task'] );
	}   
   if (isset($query['Itemid'])) {
      $segments[] = $query['Itemid'];
      unset( $query['Itemid'] );
   }
   if (isset($query['id'])) {
      $segments[] = $query['id'];
      unset( $query['id'] );
   }  
	
	if(!empty($query)){
		foreach($query as $name => $value){
			if($name != 'option'){
				$segments[] = $name.':'.$value;
			}else{
				$newQuery[$name] = $value;
			}
		}
		$query = $newQuery;
	}

	return $segments;
}

function CommunicatorParseRoute( $segments )
{
	$vars = array();

	if(!empty($segments)){
		$i = 0;
		foreach($segments as $name){
			if(strpos($name,':')){
				list($arg,$val) = explode(':',$name);
				$vars[$arg] = $val;
			}else{
				$i++;
				if ($i == 1) $vars['task'] = $name;
				elseif($i == 2) $vars['Itemid'] = $name;
				elseif($i == 3) $vars['id'] = $name;
			}
		}
	}

	return $vars;
}